#ifndef __SOCKETTCPCLIENT_H_
#define __SOCKETTCPCLIENT_H_

#include "SocketTcp.h"
#include "com/MyThread.h"
#include "com/define.h"
#include "com/MyEvent.h"
#include "com/ListPtr.h"
#include "com/Lock.h"

class DLL_EXT_CLASS IOTcpClientNotify
{
public:
	IOTcpClientNotify(void){};
	virtual~IOTcpClientNotify(void){};
	
	virtual void OnRecvTcpServerDataNotify(const int nCmd, const int nPacketNum, const int nSendId, const char *szBuf, int nLen) = 0;
	virtual void OnDisConnTcpServerNotify(const int nType) = 0;
};

class DLL_EXT_CLASS CSocketTcpClient:public CMyThread
{
public:

	CSocketTcpClient(IOTcpClientNotify *pNotify, bool bCheckHeart = true);
	~CSocketTcpClient(void);

	bool StartConnect(const char *szIP, unsigned short usPort);
	void DisConnect(void);

	int SendData(const int nUserId, const int nCmd, int nPacketNum, const char *pData, int nLen);
	int SendDataEx(const char *pData, int nLen);

	SOCKET GetSocket(void);
	void SetCheckTime(int nTime);

	bool IsConnect();
private:
	bool CreateSocket(void);
	bool Connect(const char *szIP, unsigned short usPort);

	void HandleRecvData();
	void CheckTimeout(void);

protected:
	virtual void ThreadProcMain(void);

private:

	IOTcpClientNotify     *m_pNotify;
	CSocketTcp            *m_pConn;

	bool                  m_bExit;
	bool                  m_bCheckConn;
	bool                  m_bConnect;

	char                  *m_pRecvBuff;
	int                   m_nBuffLen;
	int                   m_nRecvDataLen;

	char                  m_szIP[16];
	unsigned short        m_usPort;

	unsigned int          m_nTimeOut;
	unsigned long         m_ulLastRecvTime;	
};
#endif

